package fr.umlv.m2.jee.pmlv.service.cursus.impl;

import fr.umlv.m2.jee.pmlv.service.planning.DisplayAbstractEvent;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import fr.umlv.m2.jee.pmlv.persistence.cursus.Cursus;
import fr.umlv.m2.jee.pmlv.persistence.cursus.dao.HibernateCursusDAO;
import fr.umlv.m2.jee.pmlv.service.cursus.DisplayCursus;
import fr.umlv.m2.jee.pmlv.service.cursus.IDisplayCursusService;

@Service("defautDisplayCursusService")
public class DefautDisplayCursusService implements IDisplayCursusService {

    @Autowired
    @Qualifier("hibernateCursusDAO")
    private HibernateCursusDAO dao;
 
    @Override
    @Transactional(readOnly=true)
    public List<DisplayCursus> getAllDisplayCursus() {
        ArrayList< DisplayCursus > result = new ArrayList< DisplayCursus >(16);
        for(Cursus cursus : dao.findAll() ) {
            result.add(new DisplayCursus(cursus));
        }
        return result;
    }

    @Override
    @Transactional(readOnly=true)
    public DisplayCursus getDisplayCursusById(String id) {
        return new DisplayCursus(dao.findById(Long.parseLong(id)));
    }

    @Override
    @Transactional(readOnly=true)
    public List<DisplayCursus> getCursusByProfessorUidAndByYear(String uid, int year) {
        ArrayList< DisplayCursus > result = new ArrayList< DisplayCursus >(16);
        for(Cursus cursus : dao.findCursusByProfessorUidAndByYear(uid,year) ) {
            result.add(new DisplayCursus(cursus));
        }
        return result;
    }

    @Override
    @Transactional(readOnly=true)
    public List<DisplayCursus> getCursusByStudentUidAndByYear(String uid, int year) {
        ArrayList< DisplayCursus > result = new ArrayList< DisplayCursus >(16);
        for(Cursus cursus : dao.findCursusByStudentUidAndByYear(uid,year) ) {
            result.add(new DisplayCursus(cursus));
        }
        return result;
    }

    @Override
    @Transactional(readOnly=true)
    public List<String> getCursusNamesByListOfDisplayAbstractEvent(List<DisplayAbstractEvent> events) {
        ArrayList< String > result = new ArrayList< String >(16);
        for(DisplayAbstractEvent event:events) {
            result.add( dao.findCursusBySubjectId( event.getSubject().getId() ).getName() );
        }
        return result;
    }
}